package com.example.PolimiAssistant.adapters;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import com.Wsdl2Code.WebServices.PolimiAssistant.UserCurrentSchedule;
import com.example.PolimiAssistant.R;
import com.example.PolimiAssistant.common.WeekDayFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by alejo on 6/28/14.
 */
public class AcademicSchedulePageAdapter extends FragmentPagerAdapter{


    /**
     * The context fo the parent activity (FragmentActivity)
     */
    private Context context;

    /**
     * The tag used for the logs.
     */
    protected final String TAG = AcademicSchedulePageAdapter.class.getSimpleName();

    /**
     * The map with the schedule of each day
     */
    private Map<String, ArrayList<UserCurrentSchedule>> scheduleList;

    public AcademicSchedulePageAdapter(Context context,
                                       FragmentManager fragmentManager,
                                       Map<String, ArrayList<UserCurrentSchedule>> scheduleList){
        super(fragmentManager);
        this.context = context;
        this.scheduleList = scheduleList;

    }

    public void setScheduleList(Map<String, ArrayList<UserCurrentSchedule>> scheduleList){
        this.scheduleList = scheduleList;
    }

    /**
     * This method instantiate the fragment for the given page, returning a
     * DummySectionFragment (defined as a static inner class below) with the
     * page number as its lone argument.
     *
     * @param position
     * @return
     */
    @Override
    public Fragment getItem(int position) {

        ArrayList<UserCurrentSchedule> daySchedule = null;

        Log.i(TAG,"The position is: "+position);

        if(scheduleList == null){
            //manage null
            Log.i(TAG,"The position is: "+position);
            Log.i(TAG,"Enter null ");

        }else if(position == 0){
            Log.i(TAG,"Get the info for Monday");
            daySchedule = scheduleList.get("Monday");

        }else if(position == 1){
            Log.i(TAG,"Get the info for Tuesday");
            daySchedule = scheduleList.get("Tuesday");

        }else if(position == 2){
            Log.i(TAG,"Get the info for Wednesday");
            daySchedule = scheduleList.get("Wednesday");

        }else if(position == 3){
            Log.i(TAG,"Get the info for Thursday");
            daySchedule = scheduleList.get("Thursday");

        }else if(position == 4){
            Log.i(TAG,"Get the info for Friday");
            daySchedule = scheduleList.get("Friday");

        }else{
            //manage error
        }

        Fragment weekDayFragment;
        weekDayFragment = new WeekDayFragment(context, daySchedule);


        return weekDayFragment;
    }


    @Override
    public CharSequence getPageTitle(int position) {

        String tabLabel = null;

        switch (position) {
            case 0:
                tabLabel = context.getResources().getString(R.string.schedule_weekday_monday);
                break;
            case 1:
                tabLabel = context.getResources().getString(R.string.schedule_weekday_tuesday);
                break;
            case 2:
                tabLabel = context.getResources().getString(R.string.schedule_weekday_wednesday);
                break;
            case 3:
                tabLabel = context.getResources().getString(R.string.schedule_weekday_thursday);
                break;
            case 4:
                tabLabel = context.getResources().getString(R.string.schedule_weekday_friday);
                break;
        }

        return tabLabel;
    }

    @Override
    public int getCount() {
        return 5;
    }
}
